package model;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * 
 * Represents various technical details of the cursor.
 * 
 * @author Chris James and Kevin Tao
 *
 */

public class Turtle extends Observable { 
	
	public BufferedImage myImage;
	public AbstractPen myPen, stockPen;
	public boolean visibility;
	protected Bearing myBearing; // Coordinate and Direction
	
	protected Trail activeTrail;
	private Model myModel;

	public Turtle(Model model) {
		myBearing = new Bearing();
		myPen = new Pen(); // default pen
		myModel = model;
		visibility = true;
		updateCurrentTrail(myBearing);
		
		try{
		
			myImage = ImageIO.read(new File("src/resources/images/Turtle.png"));
		
		}catch(IOException e){ 
			// TODO: handle exception 
		}
	}

	public void setShape(String path) {
		System.out.println("turtle.setshape got here");
		try{			
			myImage = ImageIO.read(new File(path));
		
		}catch(IOException e){ 
			System.out.println("image not found");
		} 
		myModel.notifyObservers(Model.TURTLE_OBSERVER);
	}
	
	/**
	 * Determines where the turtle will end up using trig. Also should update the path.
	 */
	public int forward(int n) {

		Bearing nextBearing = new Bearing(myBearing);

		nextBearing.translateToPoint(n);
		
		//nextBearing = myBearing.fixBearing(nextBearing, myWorld.getBounds());
				
		move(nextBearing);

		notifyObservers();
		
		return n;// no need to fix this! gonna wrap, will always go n distance
	}


	public int backward(int n) {
		return forward(-n);
	}

	public double left(double n) {
		myBearing.setDirection((myBearing.getDirection() + n) % Bearing.CIRCLE);
		return n;
	}

	public double right(double n) {
		return left(-1 * n);
	}

	public void showTurtle() {
		visibility = true;
	}

	public void hideTurtle() {
		visibility = false;
	}

	public boolean penUp() {
		stockPen = new Pen(myPen);
		changePen(new InvisiblePen());
		return false;
	}

	public boolean penDown() {
		myPen = new Pen(stockPen);
		return true;
	}

	public void changePen(AbstractPen pen){
		myModel.addTrail(activeTrail);
		activeTrail = new Trail(myPen);
		myPen = new Pen(pen);
	}
	
	public void setBearing(int x, int y, double direction) {
		myBearing = new Bearing(new Point(x,y), direction);
		myModel.notifyObservers(Model.TURTLE_OBSERVER);
	}
	
	public void setPoint(int x, int y){
		myBearing.setPoint(new Point(x,y));
	}
	
	public void move(Bearing nextBearing) {
		
		myBearing = new Bearing(nextBearing);
		// by this time you are at nextBearing
		//updateTrail adds bearing to updateTrail based on pen type
		updateCurrentTrail(myBearing);
		
	}
	
	public void updateCurrentTrail(Bearing newBearing){
		myPen.updateTrail(myBearing.getPoint());
		activeTrail = myPen.getTrail();
	}

	public Trail getActiveTrail(){
		return activeTrail;
	}
	
	public Bearing getBearing() {
		return myBearing;
	}
	
	public BufferedImage getImage(){
		return myImage;
	}
	
	@Override
	public void notifyObservers() {
		myModel.notifyObservers(Model.TURTLE_OBSERVER);
	}

}